package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.entity.service.customer.ICustomerInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.customer.CustomerInvoiceMapper;
import com.probox.common.entity.domain.customer.CustomerInvoice;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户发票信息Service业务层处理
 *
 * @author zsy
 * @date 2021-07-10
 */
@Service
@Transactional
public class CustomerInvoiceServiceImpl extends ServiceImpl<CustomerInvoiceMapper, CustomerInvoice> implements ICustomerInvoiceService {
    @Autowired
    private CustomerInvoiceMapper customerInvoiceMapper;

    @Override
    public List<CustomerInvoice> listInvoice(CustomerInvoice customerInvoice) {
        return customerInvoiceMapper.listInvoice(customerInvoice);
    }

    /**
     * 查询客户发票信息
     *
     * @param invoiceId 客户发票信息ID
     * @return 客户发票信息
     */
    @Override
    public CustomerInvoice selectCustomerInvoiceById(Integer invoiceId) {
        return this.getById(invoiceId);
    }

    /**
     * 查询客户发票信息列表
     *
     * @param customerInvoice 客户发票信息
     * @return 客户发票信息
     */
    @Override
    public List<CustomerInvoice> queryList(CustomerInvoice customerInvoice) {
        QueryWrapper<CustomerInvoice> queryWrapper = Wrappers.query();
        return customerInvoiceMapper.selectList(queryWrapper);
    }



    /**
     * 新增客户发票信息
     *
     * @param customerInvoice 客户发票信息
     * @return 结果
     */
    @Override
    public boolean insertCustomerInvoice(CustomerInvoice customerInvoice) {
        customerInvoice.setCustomerId(SecurityUtils.getCustomerUid());
        customerInvoice.setPlatformUid(SecurityUtils.getCustomerPlatformUid());
        customerInvoice.setDataStatus(DataStatus.valid.getKey());
        customerInvoice.setCreateUser(SecurityUtils.getCustomerUid());
        customerInvoice.setCreateName(SecurityUtils.getCustomerUserName());
        customerInvoice.setCreateTime(DateUtil.date());
        return this.save(customerInvoice);

    }

    /**
     * 修改客户发票信息
     *
     * @param customerInvoice 客户发票信息
     * @return 结果
     */
    @Override
    public boolean updateCustomerInvoice(CustomerInvoice customerInvoice) {
        customerInvoice.setUpdateName(SecurityUtils.getCustomerUserName());
        customerInvoice.setUpdateUser(SecurityUtils.getCustomerUid());
        customerInvoice.setUpdateTime(DateUtil.date());
        return this.updateById(customerInvoice);
    }

    /**
     * 批量删除客户发票信息
     *
     * @param invoiceIds 需要删除的客户发票信息ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerInvoiceByIds(Integer[] invoiceIds) {
        UpdateWrapper<CustomerInvoice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("INVOICE_ID",Arrays.asList(invoiceIds));
        updateWrapper.set("DATA_STATUS",DataStatus.delete.getKey());
        return this.update(updateWrapper);
    }

    /**
     * 获取当前客户的发票信息
     * @return
     */
    @Override
    public List<CustomerInvoice> getInvoiceInfo() {
        QueryWrapper<CustomerInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CUSTOMER_ID", SecurityUtils.getCustomerUid());
        queryWrapper.eq("PLATFORM_UID", SecurityUtils.getCustomerPlatformUid());
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        queryWrapper.orderByDesc("UPDATE_TIME");
        List<CustomerInvoice> customerInvoices = customerInvoiceMapper.selectList(queryWrapper);
        return customerInvoices;
    }


    /**
     * 获取当前客户的发票信息总条数
     * @return
     */
    @Override
    public Integer getInvoiceInfoCount() {
        QueryWrapper<CustomerInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CUSTOMER_ID", SecurityUtils.getCustomerUid());
        queryWrapper.eq("PLATFORM_UID", SecurityUtils.getCustomerPlatformUid());
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        Integer customerInvoicesCount = customerInvoiceMapper.selectCount(queryWrapper);
        return customerInvoicesCount;
    }


}
